package com.project.hqshorttaxi.service.impl;

import com.project.hqshorttaxi.dto.OperationLogParam;
import com.project.hqshorttaxi.entity.OperationLog;
import com.project.hqshorttaxi.mapper.OperationLogMapper;
import com.project.hqshorttaxi.service.OperationLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OperationLogServiceImpl implements OperationLogService {

    @Resource
    private OperationLogMapper operationLogMapper;

    @Override
    public OperationLog getById(Integer id) {
        return operationLogMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(OperationLog entity) throws Exception {
        operationLogMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(OperationLog entity) throws Exception {
        operationLogMapper.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) throws Exception {
        operationLogMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<OperationLog> list(OperationLogParam param) {
        Example example = new Example(OperationLog.class);
        Example.Criteria c = example.createCriteria();
        if (param.getOperation() != null) {
            c.andEqualTo("operation", param.getOperation());
        }
        if (param.getParkingNo() != null) {
            c.andEqualTo("parkingNo", param.getParkingNo());
        }
        if (!StringUtils.isEmpty(param.getPlateNumber())) {
            c.andLike("plateNumber", "%" + param.getPlateNumber() + "%");
        }
        if (!StringUtils.isEmpty(param.getCreateUser())) {
            c.andLike("createUser", "%" + param.getCreateUser() + "%");
        }
        if (param.getBeginTime() != null) {
            c.andGreaterThanOrEqualTo("createTime", param.getBeginTime());
        }
        if (param.getEndTime() != null) {
            c.andLessThanOrEqualTo("createTime", param.getEndTime());
        }
        example.orderBy("createTime").desc();
        List<OperationLog> list = operationLogMapper.selectByExample(example);
        return list;
    }
}
